#!/bin/bash

if [ $USER != root ]; then
echo "Run the script as \"sudo $0 \"" 
exit 1
fi

WEPS_INS_PATH="$HOME/.weps_update"

FORMAT="tar.bz2"
ARCHI="armel"
mkdir -p $WEPS_INS_PATH/tmp
mkdir -p $WEPS_INS_PATH/archive

ARCHIVE="$WEPS_INS_PATH/archive"
TMP="$WEPS_INS_PATH/tmp"

rm -rf $TMP/packages $TMP/required $TMP/updates 

id_top=`echo "select id from category where name='Development libraries - Required'" | mysql -uweps -hterminator weps -s`
id_sys_soft=`echo "select id from category where name='Development libraries - Required'" | mysql -uweps -hterminator weps -s`
echo "select name from package where cat_id='$id_sys_soft'"| mysql -uweps -hterminator weps -s>$TMP/required
required_count=`cat $TMP/required|wc -l`
j=1
exec 3<&0
exec 0< $TMP/required

(
while read -r line
do
	echo "$[$j*100/$required_count]"
	echo "# Checking $line"
	j=$[$j+1]

	NAME=$line
	#for i in `echo "select DISTINCT(pac_id) from file where name RLIKE ('$NAME')" | mysql -uweps -hterminator weps -s`
	for i in `echo "select DISTINCT(id) from package where name='$NAME' && cat_id='$id_sys_soft'" | mysql -uweps -hterminator weps -s`
	do
		PACKAGE=`echo "select name from file where id IN (select max(id) from file where pac_id=$i)" | mysql -uweps -hterminator weps -s`
		BASENAME=`basename $PACKAGE`
		PKGNAME=`echo $BASENAME | awk -F "-" '{ pkg=$1; for(f=2; f<=NF-2; f++) { pkg=pkg"-"$f } ; ver=NF-1; printf("%s\n", pkg);}'`
		if [[ $PACKAGE =~ $FORMAT ]] && [[ $PACKAGE =~ $ARCI ]] ; then
			echo $PKGNAME
			if [ -f $ARCHIVE/$PKGNAME ]
				then
				if [ $BASENAME != "`cat $ARCHIVE/$PKGNAME`" ]
					then
					echo "http://terminator/var/www/weps/repository/GeoAmida-Alpha/Development%20libraries%20-%20Required/$PKGNAME/$PACKAGE">>$TMP/packages
					echo -e "\n$BASENAME\n`cat $ARCHIVE/$PKGNAME`">>$TMP/updates
				fi
			else
				#echo "$BASENAME">wepsupdate/$PKGNAME
				echo "http://terminator/var/www/weps/repository/GeoAmida-Alpha/Development%20libraries%20-%20Required/$PKGNAME/$PACKAGE">>$TMP/packages
				echo -e "\n$BASENAME\n*New*">>$TMP/updates
			fi
			#wget --directory-prefix=$1 http://terminator/var/www/weps/repository/$4/System%20Software/$PKGNAME/$PACKAGE
		fi
	done

done
)|zenity --progress \
		--auto-close \
		--title="Reading Terminator weps" \
		--text="comparing packages" \
		--percentage=0
update_count=$[`cat $TMP/updates|wc -l`/3+1]
exec 0<&3
files=`cat $TMP/updates | zenity --list --width=600 --height=300 --title "Select packages to update($update_count Total updates)" \
				--column " " --column "Weps Package" --column "PC Package" \
				--separator=" " --multiple --checklist \
				--print-column='2'`
file_count=`echo $files|wc -w`
j=1
(
for i in $files 
	do
		echo "$[$j*100/$file_count]"
		echo "Downloading $i"
		wget --directory-prefix=$TMP `cat $TMP/packages | grep $i`
		j=$[$j+1]
done
)|zenity --progress \
		--auto-close \
		--title="Downloading packages." \
		--text="Downloading packages" \
		--percentage=0
j=1
(
for i in $files
	do
		echo "$[$j*100/$file_count]"
		echo "# Untar $i"
		tar xvf  $TMP/$i  -C /
		PKGNAME=`echo $i | awk -F "-" '{ pkg=$1; for(f=2; f<=NF-2; f++) { pkg=pkg"-"$f } ; ver=NF-1; printf("%s\n", pkg);}'`
		echo "$i">$ARCHIVE/$PKGNAME
		j=$[$j+1]
	done
	)|zenity --progress \
			--auto-close \
			--title="Update progress" \
			--text="Untarring packages" \
			--percentage=0
echo "Done"
zenity --info \
	  --text="Updation compleated"

